package com.apes.fn.fico.debitnote.service;

import com.apes.fn.fico.debitnote.model.BusinessDebitNote;
import com.apes.fn.fico.debitnote.model.DebitRate;
import com.apes.fn.fico.debitnote.repository.DebitRateRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Creator: zouyc
 * CreateDate: 2019-12-23 9:12
 * Description:
 **/
@Service("debitRateService")
public class DebitRateService extends DomainService {
    @Autowired
    DebitRateRepository debitRateRepository;

    public DebitRate create(SimpleRequest request){
        DebitRate debitRate= request.getO(DebitRate.class);
        debitRate = save(debitRate);
        return debitRate;
    }

    private DebitRate save(DebitRate debitRate) {
        debitRate = debitRateRepository.save(debitRate);
        return debitRate;
    }

    /**
     * 根据营业款借支单计算日息
     */
    public DebitRate dailyCreate(BusinessDebitNote businessDebitNote){
        Double annualRate = businessDebitNote.getAnnualRate();
        Double capitalBalanceOrigin = businessDebitNote.getCapitalBalance();
        Double interestBalanceOrigin = businessDebitNote.getInterestBalance();
        Double dailyRate = capitalBalanceOrigin*(annualRate/360); //本日利息

        DebitRate debitRate = new DebitRate();
        debitRate.setAnnualRate(annualRate);
        debitRate.setBusinessDebitNote(businessDebitNote);
        debitRate.setTodayInterest(dailyRate);
        debitRate.setCapitalBalance(capitalBalanceOrigin);
        debitRate.setInterestBalance(interestBalanceOrigin + dailyRate);
        debitRate.setCreator("yyadmin");
        debitRate = save(debitRate);

        return debitRate;
    }

}
